<?php
    require '../../../../inc/config/config.php';
    require '../../../../inc/library/functions.php';
	$error = "";
	$msg = " ";
	$filetmpname=$_FILES['ToUpload']['tmp_name'];
	$filename=$_FILES['ToUpload']['name'];
	$uploads=dirname(__FILE__)."/".UPLOADPATH;
	$ext=strtolower(substr($filename,strrpos($filename,".")+1));
	if(!empty($_FILES['ToUpload']['error']))
	{
		switch($_FILES['ToUpload']['error'])
		{

			case '1':
				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$error = '请选择要上传的文件';
				break;

			case '6':
				$error = 'Missing a temporary folder';
				break;
			case '7':
				$error = 'Failed to write file to disk';
				break;
			case '8':
				$error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$error = 'No error code avaiable';
		}
	}else if($ext!= 'gif' and $ext!= 'jpg' and $ext!= 'png' and $ext!="bmp"){
		$error = '只允许上传jpg,png,gif,bmp等格式文件';
	}else {
			//$msg .= " File Name: " . $_FILES['fileToUpload']['name'] . ", ";
			//$msg .= " File Size: " . @filesize($_FILES['fileToUpload']['tmp_name']);
			//for security reason, we force to remove all uploaded file
			//@unlink($_FILES['fileToUpload']);
            $times=time();
			$names=$times.".".$ext;
			$newfilename=$uploads.$names;
            $tumbnailnames=$times."_th.".$ext;
			if(move_uploaded_file($filetmpname,$newfilename)){	
				$msg.="上传成功";
				$result="/uploads/".$names;
                ImageResize($newfilename,220,168,$uploads.$tumbnailnames);
			}else{
				$msg.="上传失败";
			}
				 		
	}		
	echo "{";
	echo				"error: '" . $error . "',\n";
	echo				"msg: '" . $msg . "',\n";
	echo				"result: '" . $result . "'\n";
	echo "}";
?>